package com.my.mygaode

import android.util.Log
import com.amap.api.location.AMapLocation
import com.amap.api.location.AMapLocationClient
import com.amap.api.location.AMapLocationClientOption
import com.amap.api.location.AMapLocationListener

/**
 * @ClassName LocationHelper
 * @Description TODO
 * @Author mwj
 * @Date 2023/6/19 14:47
 * @Version 1.0
 **/
class LocationHelper private constructor() : AMapLocationListener {


    companion object {
        val instance: LocationHelper by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            LocationHelper()
        }
    }

    private var aMapLocationClient: AMapLocationClient? = null

    private var clientOption: AMapLocationClientOption? = null

    private var callBackList = ArrayList<ILocationCallBack>()

     fun initLocation(){
         AMapLocationClient.updatePrivacyShow(BaseApplication.get(), true, true)
         AMapLocationClient.updatePrivacyAgree(BaseApplication.get(), true)
        aMapLocationClient = AMapLocationClient(BaseApplication.get())
        //设置监听回调
        aMapLocationClient?.setLocationListener(this)
        //初始化定位参数
        clientOption = AMapLocationClientOption()
        clientOption?.locationMode = AMapLocationClientOption.AMapLocationMode.Battery_Saving
        clientOption?.isNeedAddress = true
        //单次监听
        clientOption?.isOnceLocation = true
        //设置是否强制刷新WIFI，默认为强制刷新
        clientOption?.isWifiActiveScan = true
        //设置是否允许模拟位置,默认为false，不允许模拟位置
        clientOption?.isMockEnable = false
        //多次监听，设置定位间隔
        clientOption?.interval = 2000
        aMapLocationClient?.setLocationOption(clientOption)
    }

    fun startLocate() {
        aMapLocationClient?.startLocation()
    }

    //完成定位回调
    override fun onLocationChanged(aMapLocation: AMapLocation?) {
        if (aMapLocation != null) {
            if (aMapLocation.errorCode == 0) {
                //定位成功完成回调
                val adCode = aMapLocation.adCode
                val country = aMapLocation.country
                val province = aMapLocation.province
                val city = aMapLocation.city
                val district = aMapLocation.district
                val street = aMapLocation.street
                val lat = aMapLocation.latitude
                val lgt = aMapLocation.longitude
                Log.i("mygaode", "位置信息--->$aMapLocation")
                callBack(adCode, district, lat, lgt, aMapLocation)
            } else {
                //显示错误信息ErrCode是错误码，errInfo是错误信息，详见错误码表。
                Log.e(
                    "mygaode", "location Error, ErrCode:"
                            + aMapLocation.errorCode + ", errInfo:"
                            + aMapLocation.errorInfo
                )
            }
        }
    }

    private fun callBack(
        code: String?,
        str: String?,
        lat: Double,
        lgt: Double,
        aMapLocation: AMapLocation?
    ) {
        callBackList?.forEach {
            it.callBack(code, str, lat, lgt, aMapLocation)
        }
    }


    interface ILocationCallBack {
        fun callBack(
            code: String?,
            str: String?,
            lat: Double,
            lgt: Double,
            aMapLocation: AMapLocation?
        )
    }

    fun addLocationCallBack(callBack: ILocationCallBack) {
        callBackList.add(callBack)
    }

    fun removeLocationCallBack(callBack: ILocationCallBack) {
        if (callBackList.contains(callBack)) {
            callBackList.remove(callBack)
        }
    }

    /**
     * 停止定位
     */
    fun stopLocation() {
        if (aMapLocationClient?.isStarted == true) {
            aMapLocationClient?.stopLocation()
        }
    }

    /**
     * 销毁操作
     */
    fun destroy() {
        stopLocation()
        callBackList.clear()
        aMapLocationClient?.setLocationListener(null)
        aMapLocationClient?.onDestroy()
        aMapLocationClient = null
    }
}
